package com.liao.harmony.viewmodel;

import com.liao.harmony.base.BaseViewModel;
import com.liao.harmony.bean.BaseResponse;
import com.liao.harmony.bean.TestDataBean;
import com.liao.harmony.http.HttpHelper;
import com.liao.library.http.subscriber.CommonSubscriber;
import com.liao.library.lifecycle.livedata.MutableLiveData;
import com.liao.library.rx_harmony.RxScheduler;
import io.reactivex.rxjava3.annotations.NonNull;
import io.reactivex.rxjava3.disposables.Disposable;
import ohos.aafwk.ability.AbilityPackage;

import java.util.List;

public class MainViewModel extends BaseViewModel {
    private Disposable disposable;
    public MutableLiveData<List<TestDataBean>> data = new MutableLiveData<>();

    public MainViewModel(AbilityPackage mApplication) {
        super(mApplication);
    }

    public String getText(){
        return "来自ViewModel的Text";
    }

    public void requestNetWork(){
        disposable = HttpHelper.getApi().getTestData()
                .compose(RxScheduler.Flo_io_main())
                .subscribeWith(new CommonSubscriber<BaseResponse<List<TestDataBean>>>() {
//                    @Override
//                    protected void onStart() {
//                        super.onStart();
//                        //这里可以告知UI启动Loading弹框
//                    }

                    @Override
                    protected void onSuccess(@NonNull BaseResponse<List<TestDataBean>> listBaseResponse) {
                        data.setValue(listBaseResponse.getData());
                    }

                    @Override
                    protected void onFail(String msg) {
                        //异常
                    }

                    @Override
                    protected void onFinish() {
                        //请求结束
                    }
                });
    }

    @Override
    protected void onCleared() {
        super.onCleared();
        if (disposable != null && !disposable.isDisposed()){
            disposable.dispose();
        }
    }
}
